﻿using UnityEngine;
using System.Collections;

using XInputDotNetPure; // Required in C#


public class EndGameScript : MonoBehaviour {
	
	bool playerIndexSet = false;
	PlayerIndex playerIndex;
	GamePadState state;
	GamePadState prevState;
	
	public GameObject levelTextNr;
	
	int level = 1;
	
	// Use this for initialization
	void Start () 
	{
		FindPlayerIndex();
		
	}
	
	bool thumbPressed = false;
	
	void SetLevel(int newlevel)
	{
		if(newlevel < 1) newlevel = Application.levelCount-1;
		if(newlevel > Application.levelCount-1) newlevel = 1;
		
		level = newlevel;
		UnityEngine.UI.Text txt = (UnityEngine.UI.Text)levelTextNr.GetComponent(typeof( UnityEngine.UI.Text ));
		
		txt.text = "< " + level.ToString() + " >";// ' H');
		
		
	}
	
	void FindPlayerIndex()
	{
		// Find a PlayerIndex, for a single player game
		// Will find the first controller that is connected ans use it
		if (!playerIndexSet || !prevState.IsConnected)
		{
			for (int i = 0; i < 4; ++i)
			{
				PlayerIndex testPlayerIndex = (PlayerIndex)i;
				GamePadState testState = GamePad.GetState(testPlayerIndex);
				if (testState.IsConnected)
				{
					Debug.Log(string.Format("GamePad found {0}", testPlayerIndex));
					playerIndex = testPlayerIndex;
					playerIndexSet = true;
				}
			}
		}
		
		prevState = state;
		state = GamePad.GetState(playerIndex);
		
	}
	
	// Update is called once per frame
	void Update () 
	{
		prevState = state;
		state = GamePad.GetState(playerIndex);
		
		
		if(state.Buttons.A == ButtonState.Pressed)
		{
			Application.LoadLevel(0);
		}

		
		
	}
}
